package com.diet.web.controller.diet;

import com.diet.common.annotation.Log;
import com.diet.common.core.controller.BaseController;
import com.diet.common.core.domain.AjaxResult;
import com.diet.common.core.page.TableDataInfo;
import com.diet.common.enums.BusinessType;
import com.diet.common.utils.poi.ExcelUtil;
import com.diet.system.domain.Ingredient;
import com.diet.system.service.IIngredientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 食材Controller
 */
@RestController
@RequestMapping("/diet/ingredient")
public class IngredientController extends BaseController
{
    @Autowired
    private IIngredientService ingredientService;

    /**
     * 查询食材列表
     */
    @PreAuthorize("@ss.hasPermi('diet:ingredient:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ingredient ingredient)
    {
        startPage();
        List<Ingredient> list = ingredientService.selectIngredientList(ingredient);
        return getDataTable(list);
    }

    /**
     * 导出食材列表
     */
    @PreAuthorize("@ss.hasPermi('diet:ingredient:export')")
    @Log(title = "食材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ingredient ingredient)
    {
        List<Ingredient> list = ingredientService.selectIngredientList(ingredient);
        ExcelUtil<Ingredient> util = new ExcelUtil<Ingredient>(Ingredient.class);
        util.exportExcel(response, list, "食材数据");
    }

    /**
     * 获取食材详细信息
     */
    @PreAuthorize("@ss.hasPermi('diet:ingredient:query')")
    @GetMapping(value = "/{ingredientId}")
    public AjaxResult getInfo(@PathVariable("ingredientId") Long ingredientId)
    {
        return AjaxResult.success(ingredientService.selectIngredientByIngredientId(ingredientId));
    }

    /**
     * 新增食材
     */
    @PreAuthorize("@ss.hasPermi('diet:ingredient:add')")
    @Log(title = "食材", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ingredient ingredient)
    {
        return toAjax(ingredientService.insertIngredient(ingredient));
    }

    /**
     * 修改食材
     */
    @PreAuthorize("@ss.hasPermi('diet:ingredient:edit')")
    @Log(title = "食材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ingredient ingredient)
    {
        return toAjax(ingredientService.updateIngredient(ingredient));
    }

    /**
     * 删除食材
     */
    @PreAuthorize("@ss.hasPermi('diet:ingredient:remove')")
    @Log(title = "食材", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ingredientIds}")
    public AjaxResult remove(@PathVariable Long[] ingredientIds)
    {
        return toAjax(ingredientService.deleteIngredientByIngredientIds(ingredientIds));
    }
}